package com.wdh.testq.tools;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;


import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Gss on 2019/3/19.
 */
public class PermissionTools {
    public static final String CHECK_CAMERA = Manifest.permission.CAMERA;
    public static final String CHECK_READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;
    public static final String ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
    public static final String ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    public static final String WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    public static final String REQUEST_INSTALL_PACKAGES = Manifest.permission.REQUEST_INSTALL_PACKAGES;
    public static final String READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE;
    public static final String CHECK_READ_CALL_LOG = Manifest.permission.READ_CALL_LOG;
    public static final String CHECK_READ_CONTACTS = Manifest.permission.READ_CONTACTS;
    public static final String CHECK_WRITE_CONTACTS = Manifest.permission.WRITE_CONTACTS;
    public static final String CHECK_SYSTEM_ALERT_WINDOW = Manifest.permission.SYSTEM_ALERT_WINDOW;
    public static final String CHECK_WRITE_SETTINGS = Manifest.permission.WRITE_SETTINGS;
    public static final String CHECK_GET_ACCOUNTS = Manifest.permission.GET_ACCOUNTS;
    public static final String CHECK_READ_SMS = Manifest.permission.READ_SMS;
    public static final String CHECK_SEND_SMS = Manifest.permission.SEND_SMS;
    public static final String CHECK_RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
    public static final String CALL_PHONE = Manifest.permission.CALL_PHONE;
    private static final int PERMISSION_REQUEST_CODE = 10000;
    private static PermissionTools permissionUtils;
    private Context mContext;
    private CallBack callBack;
    List<Resultful> list = new ArrayList<>();
    ArrayList<String> pers = new ArrayList<>();

    /**
     * 单例获取实例
     */
    public static PermissionTools getInstance() {
        if (permissionUtils == null) {
            permissionUtils = new PermissionTools();
        }
        return permissionUtils;
    }

    /**
     * 绑定Activity，在下面会强转
     */
    public PermissionTools bind(Context context) {
        mContext = context;
        return this;
    }

    /**
     * 回调结束后销毁
     */
    private void destroyContext() {
        mContext = null;
    }

    /**
     * 检查单项权限是否赋予
     */
    public boolean checkPermission(String permission) {
        boolean hasPermission;
        if (ContextCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED) {
            hasPermission = false;
        } else {
            hasPermission = true;
        }
        return hasPermission;
    }

    /**
     * 遍历请求数组中的所有权限，尚未拥有的权限则调用申请权限方法，如果全都已有，则调用permisionExist
     */
    public PermissionTools permission(String... permissions) {
        pers.clear();
        for (String per : permissions) {
            if (!checkPermission(per)) {
                pers.add(per);
            }
        }
        return this;
    }

    public PermissionTools call(CallBack callBack) {
        this.callBack = callBack;
        return this;
    }

    public void excute() {
        if (pers.size() != 0) {
            String[] reqPer = pers.toArray(new String[pers.size()]);
            ActivityCompat.requestPermissions(((Activity) mContext), reqPer, PERMISSION_REQUEST_CODE);
        } else {
            if (callBack != null) {
                callBack.onAction();
                callBack = null;
            }
        }
    }

    public void onRequestPermissionsResult(String[] permissions, int[] grantResults) {
        list.clear();
        destroyContext();
        if (callBack == null)
            return;
        if (permissions.length > 0) {
            for (int i = 0; i < permissions.length; i++) {
                if (i == grantResults.length)
                    break;
                list.add(new Resultful(permissions[i], grantResults[i] == PackageManager.PERMISSION_GRANTED));
            }
            callBack.onAction(list);
        } else
            callBack.onAction();
        callBack = null;
    }
}
